package com.shuai.analysis.service.inner;

import com.alibaba.nacos.api.utils.StringUtils;
import com.shuai.analysis.domain.entity.ChartInfo;
import com.shuai.analysis.service.ChartInfoService;
import com.shuai.common.constant.ErrorCode;
import com.shuai.common.dto.ChartQueryDTO;
import com.shuai.common.dto.ChartUpdateDTO;
import com.shuai.common.exception.BusinessException;
import com.shuai.common.service.analysis.InnerAnalysisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
@DubboService
@Slf4j
public class InnerAnalysisServiceImpl implements InnerAnalysisService {
    @Autowired
    private ChartInfoService chartInfoService;

    public ChartQueryDTO getChartInfo(String chartId) {
        if (StringUtils.isBlank(chartId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "查根据ID询图表信息时id不能为空");
        }
        ChartInfo chartInfo = chartInfoService.getById(chartId);
        if (Objects.isNull(chartInfo)) {
            return null;
        }
        ChartQueryDTO chartQueryDTO = new ChartQueryDTO();
        BeanUtils.copyProperties(chartInfo, chartQueryDTO);
        return chartQueryDTO;
    }

    @Override
    public boolean updateChartInfo(ChartUpdateDTO chartUpdateDTO) {
        if (chartUpdateDTO == null || chartUpdateDTO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新图表信息时id不能为空");
        }
        ChartInfo chartInfo = new ChartInfo();
        BeanUtils.copyProperties(chartUpdateDTO, chartInfo);
        return chartInfoService.updateById(chartInfo);
    }
}
